<?php
namespace Admin\Model;
use Think\Model;

class AdModel extends Model {
    
    protected $_validate = array(
        array('title', 'require', '标题不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('title', '1,80', '标题长度不能超过80个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
        array('position', 'checkPosition', '找不到对应广告位置', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
        array('product_id', 'checkProduct', '找不到对应商品', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
        array('begin_time', 'date', '开始时间格式错误', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('end_time', 'date', '结束时间格式错误', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH)
    );
    
    protected $_auto = array(
        array('begin_time', 'getBeginTime', self::MODEL_BOTH, 'callback'),
        array('end_time', 'getEndTime', self::MODEL_BOTH, 'callback'),
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_BOTH)
    );
    
    protected function checkPosition() {
        $position = I('post.position');
        return getAdPositions($position) ? true : false; 
    }
    
    protected function checkProduct() {
        $product_id = I('post.product_id');
        $productInfo = M('product')->find($product_id);
        return $productInfo ? true : false;
    }
    
    protected function getBeginTime($str) {
        $str = trim($str) . " 00:00:00";
        return strtotime($str);
    }
    
    protected function getEndTime($str) {
        $str = trim($str) . " 23:59:59";
        return strtotime($str);
    }
    
}